package com.xyht.medicine_management_system.modules.user.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.medicine_management_system.modules.user.entity.MmsUser;
import com.xyht.medicine_management_system.modules.user.mapper.MmsUserMapper;
import com.xyht.medicine_management_system.modules.user.util.verify_code.RandomUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

@Component
public class InvitationCodeUtil {
    @Resource
    private MmsUserMapper mmsUserMapper;

    public String generateInvitationCode() {
        return generateInvitationCode(null);
    }
    public String generateInvitationCode(String code) {
        if (!isNullOrEmpty(code)) {
            MmsUser mmsUser = mmsUserMapper.selectOne(new LambdaQueryWrapper<MmsUser>()
                    .eq(MmsUser::getInvitationCode, code));
            if (mmsUser == null) {
                return code;
            }
        }

        String randomCode = RandomUtils.randomString(8);
        return generateInvitationCode(randomCode);
    }

}
